package com.sina.shop.manage.service.goods;

import com.sina.shop.core.exception.client.DataException;
import com.sina.shop.core.exception.server.CreateException;
import com.sina.shop.core.exception.server.ResourceExistsException;
import com.sina.shop.core.exception.server.UpdateException;
import com.sina.shop.domain.goods.GoodsPhoto;
import com.sina.shop.manage.dao.goods.GoodsPhotoManageDao;
import com.sina.shop.service.ShopDataSourceService;
import com.sina.shop.util.Assert;
import com.sina.shop.util.ResultCode;
import com.sina.shop.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

import static com.sina.shop.service.ServiceConstant.SHOP_TX;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * 段浩杰   2018-01-15-15:07
 */
@Service("goodsPhotoManageService")
public class GoodsPhotoManageServiceImpl extends ShopDataSourceService implements GoodsPhotoManageService {

    @Autowired
    @Qualifier("goodsPhotoManageDao")
    private GoodsPhotoManageDao goodsPhotoManageDao;

    @Override
    @Transactional(value = SHOP_TX, readOnly = true, isolation = Isolation.READ_COMMITTED)
    public GoodsPhoto getGoodsPhotoById(Long id) {
        return goodsPhotoManageDao.get(GoodsPhoto.class, id);
    }

    @Override
    @Transactional(value = SHOP_TX, isolation = Isolation.REPEATABLE_READ)
    public int deleteGoodsPhoto(Long id) throws DataException, UpdateException {
        GoodsPhoto goodsPhoto = goodsPhotoManageDao.get(GoodsPhoto.class, id);
        Assert.assertNotNull(goodsPhoto, "GoodsPhoto[id=%s] not exists", goodsPhoto.getId());

        try {
            return goodsPhotoManageDao.deleteGoodsPhoto(id);
        } catch (DataAccessException e) {
            LOG.error(" {} delete GoodsPhoto error.", GoodsPhoto.class, e);
            throw new UpdateException(ResultCode.dataAccessError, e.getMessage(), e);
        }
    }

    @Override
    @Transactional(value = SHOP_TX, isolation = Isolation.READ_COMMITTED)
    public GoodsPhoto saveGoodsPhoto(GoodsPhoto goodsPhoto) throws CreateException, ResourceExistsException, DataException {
        boolean exists;
        exists = goodsPhotoManageDao.isExistsByUnique(GoodsPhoto.class, "name", goodsPhoto.getName());
        Assert.assertFalse(exists, ResourceExistsException.class, ResultCode.dataExists
                , "GoodsPhoto[%s] exists", goodsPhoto.getName());

        try {
            goodsPhotoManageDao.save(goodsPhoto);
            goodsPhotoManageDao.flush();
            return goodsPhoto;
        } catch (DataAccessException e) {
            LOG.error(" {} save GoodsPhoto error.", GoodsPhoto.class, e);
            throw new UpdateException(ResultCode.dataAccessError, e.getMessage(), e);
        }
    }

    @Override
    @Transactional(value = SHOP_TX, isolation = Isolation.REPEATABLE_READ)
    public GoodsPhoto updateGoodsPhoto(GoodsPhoto goodsPhoto) throws UpdateException, DataException {
        GoodsPhoto g = goodsPhotoManageDao.get(GoodsPhoto.class, goodsPhoto.getId());
        Assert.assertNotNull(g, "GoodsPhoto[id=%s] not exists", goodsPhoto.getId());

        try {
            g = updateGoodsPhotoFiled(goodsPhoto, g);
            goodsPhotoManageDao.flush();

            return g;
        } catch (DataAccessException e) {
            LOG.error(" {} update GoodsPhoto error.", GoodsPhoto.class, e);
            throw new UpdateException(ResultCode.dataAccessError, e.getMessage(), e);
        }
    }

    private GoodsPhoto updateGoodsPhotoFiled(GoodsPhoto oldGoodsPhoto, GoodsPhoto newGoodsPhoto) {

        if (!StringUtils.isEmpty(oldGoodsPhoto.getName())) {
            newGoodsPhoto.setName(newGoodsPhoto.getName());
        }
        if (oldGoodsPhoto.getSort() != null) {
            newGoodsPhoto.setSort(oldGoodsPhoto.getSort());
        }
        /*if (newGoodsPhoto.getGoods() != null) {
            newGoodsPhoto.setGoods(oldGoodsPhoto.getGoods());
        }*/

        return newGoodsPhoto;
    }

    @Override
    @Transactional(value = SHOP_TX, readOnly = true, isolation = Isolation.READ_COMMITTED)
    public List<Map<String, Object>> getGoodsPhotoList(GoodsPhoto goodsPhoto, Long[] count) {

        if (count != null && count.length > 0) {
            count[0] = goodsPhotoManageDao.getGoodsPhotoCount(goodsPhoto);
        }

        return goodsPhotoManageDao.getGoodsPhotoList(goodsPhoto);
    }
}
